<template>
<el-row type='flex' justify='center' align='middle' class="login">
  <!-- <el-col :span="8"> -->
  <el-col :xs="14" :sm="12" :md="10" :lg="8" :xl="7">
    <el-form :model="loginForm" :rules="rules" ref="loginForm" label-position='top' label-width="100px" class="login-Form">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="loginForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="loginForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
        <el-button @click="resetForm('loginForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-col>
</el-row>

</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '用户名长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '密码长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      this.$refs.loginForm.validate((valid) => {
        if (!valid) {
          return false
        }
        // 校验成功时，跳转页面，保存token
        axios.post('http://localhost:8888/api/private/v1/login', this.loginForm)
          .then(res => {
            console.log(res.data)
            if (res.data.meta.status === 200) {
            // 编程式导航
              // 保存token
              localStorage.setItem('token', res.data.data.token)
              // 提示信息
              this.$message({
                message: '登录成功',
                type: 'success',
                duration: 700
              })
              this.$router.push('/home')
            } else {
            // this.$message.error('错了哦，这是一条错误消息');
              this.$message({
                message: '登录失败',
                type: 'error',
                duration: 900
              })
            }
          })
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style>
.login {
  height: 100%;
  background: #2d434c;
}
.login-Form {
  background: #fff;
  padding: 20px;
  border-radius: 15px;
}
</style>
